﻿#region Usings

using System;
using System.Globalization;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;

#endregion

namespace ScrumTable.UI.View.WinTouch.CustomControl
{
    /// <summary>
    /// DigitBox
    /// </summary>
    public class SurfacePopup : Popup
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
      
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
     
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        public SurfacePopup()
        { 
        }
        #endregion




        #region Methods

        protected override void OnOpened(EventArgs e)
        {
            HwndSource hwndSource = PresentationSource.FromVisual((Visual)VisualTreeHelper.GetParent(Child)) as HwndSource;

            // Enable surface input and touch events
            hwndSource.EnableSurfaceInput();

            // Disable visual tablet feedback
            NativeMethods.EnableTabletGestures(hwndSource.Handle, false);
            base.OnOpened(e);
        }

        public class NativeMethods
        {
            public static bool EnableTabletGestures(IntPtr hWnd, bool enable)
            {
                string atom = "MicrosoftTabletPenServiceProperty";
                long num = (long)NativeMethods.GlobalAddAtom(atom);
                if (num == 0L)
                {
                    return false;
                }
                if (enable)
                {
                    return NativeMethods.RemoveProp(hWnd, atom).ToInt64() == 1L;
                }
                int value = 16842777;
                return NativeMethods.SetProp(hWnd, atom, new IntPtr(value)) == 1;
            }

            // Microsoft.Surface.Presentation.Common.NativeMethods
            [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
            public static extern short GlobalAddAtom(string atom);

            // Microsoft.Surface.Presentation.Common.NativeMethods
            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            public static extern IntPtr RemoveProp(IntPtr hWnd, string atom);

            // Microsoft.Surface.Presentation.Common.NativeMethods
            [DllImport("user32.dll", CharSet = CharSet.Unicode)]
            public static extern int SetProp(IntPtr hWnd, string atom, IntPtr handle);
        }


        #endregion
        #region Events

        #endregion
    }
}
